package base.world;

import java.awt.event.KeyEvent;
import java.util.ArrayList;

import base.Start;
import base.input.IInput;
import base.input.InputHandler;
import base.render.gui.GuiButton;

public abstract class WorldOfButtons extends World implements IInput
{
	private int buttonCurrentID;
	private boolean allowButtonUpdate = true;
	private ArrayList<GuiButton> buttonList = new ArrayList<GuiButton>();
	
	private InputHandler theInput = new InputHandler(Start.getWindow());

	public WorldOfButtons()
	{
		this.theInput.registerKey(KeyEvent.VK_UP, (IInput) this);
		this.theInput.registerKey(KeyEvent.VK_DOWN, (IInput) this);
		this.theInput.registerKey(KeyEvent.VK_ENTER, (IInput) this);
	}
	
	public GuiButton addButton(GuiButton par1GuiButton)
	{
		this.livingCreate(par1GuiButton);
		this.buttonList.add(par1GuiButton);
		return par1GuiButton;
	}
	
	public int getUniqueButtonID()
	{
		return this.buttonList.size();
	}
	
	public void onInputUpdate(int par1, boolean par2)
	{
		if (!allowButtonUpdate)
		{
			return;
		}
		
		switch(par1)
		{
		case KeyEvent.VK_UP:
			if (!par2)
				this.toPrevButton();
			break;
		case KeyEvent.VK_DOWN:
			if (!par2)
				this.toNextButton();
			break;
		case KeyEvent.VK_ENTER:
			if (!par2)
			{
				this.buttonList.get(getCurrentButtonID()).doUpdate(1);
				this.setAllowButtonUpdate(false);
			}
			break;
		}
	}
	
	protected void toNextButton()
	{
		this.buttonCurrentID++;
		this.buttonCurrentID %= this.buttonList.size();
	}

	protected void toPrevButton()
	{
		this.buttonCurrentID--;
		if (this.buttonCurrentID < 0)
		{
			this.buttonCurrentID = this.buttonList.size() - 1;
		}
	}

	public int getCurrentButtonID()
	{
		return this.buttonCurrentID;
	}
	
	public InputHandler getInputObj()
	{
		return this.theInput;
	}
	
	public void setAllowButtonUpdate(boolean par1)
	{
		this.allowButtonUpdate = par1;
	}
}
